package net.sudot.camel;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;

import java.io.IOException;
import java.io.InputStream;

/**
 * HTTP响应消息转换
 *
 * @author tangjialin on 2020-03-24.
 */
public class HttpResponseTransform implements Expression {
    @Override
    public <T> T evaluate(Exchange exchange, Class<T> type) {
        try (InputStream in = exchange.getIn().getBody(InputStream.class)) {
            return in.available() > 0 ? (T) JsonUtils.toObject(in, ObjectNode.class) : null;
        } catch (IOException e) {
            throw new IllegalArgumentException("数据接收失败", e);
        }
    }
}